import axios from 'axios'
const http = axios.create({
  baseURL: 'http://air.airdaas.com'
})

http.interceptors.response.use(function (response) {
  var res = response.status
  if (res !== 200) {
    throw new Error('网络出问题了请稍后再试')
  }
  var code = response.data.meta.code
  var str = ''
  switch (code) {
    case 50001:
      str = '用户名或密码不正确'
      throw new Error(str)
    case 50003:
      str = 'sign未通过验证'
      throw new Error(str)
    case 50004:
      str = '用户名未填'
      throw new Error(str)
    case 50005:
      str = '密码未填'
      throw new Error(str)
    case 50006:
      str = '提交时间未填证'
      throw new Error(str)
    case 50007:
      str = '请求超时'
      throw new Error(str)
    case 50008:
      str = 'sign未填'
      throw new Error(str)
    case 50009:
      str = '该ip今日请求次数已达到上限'
      throw new Error(str)
    case 50010:
      str = '系统异常'
      throw new Error(str)
    case 50012:
      str = '用户信息错误 请重新登录'
      window.localStorage.removeItem('token')
      throw new Error(str)
    case 50016:
      str = 'min_time输入有误'
      throw new Error(str)
    case 50017:
      str = 'max_time输入有误'
      throw new Error(str)
    case 50018:
      str = '时间范围有误'
      throw new Error(str)
    case 50019:
      str = 'parameter参数不在范围内'
      throw new Error(str)
    case 50020:
      str = '该设备不属于你'
      throw new Error(str)
    case 50022:
      str = '账号不存在'
      throw new Error(str)
  }
  return response
}, function (error) {
  return Promise.reject(error)
})

export default http
